package com.yl.chainStore.control.system

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.system.CompanyDictionary
import com.yl.chainStore.pojo.vo.system.CompanyDictionaryVo
import com.yl.chainStore.services.ICompanyDictionaryService
import com.yl.chainStore.services.IWebCompanyService
import com.yl.chainStore.services.IWebSystemService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

/**
 * @description :公司字典
 * @author :吴枫
 * @create :2021-07-06 11:09:00
 */
@RestController
@RequestMapping("company/dictionary")
open class CompanyDictionaryController(
    private val companyDictionaryService: ICompanyDictionaryService,
    private val webCompanyService: IWebCompanyService,
    private val iWebSystemService: IWebSystemService
) : BaseCtrl() {

    /**
     * 增加数据字典
     */
    @PostMapping("addDictionary")
    @Transactional(rollbackFor = [Exception::class])
    open fun addDictionary(@RequestBody company: CompanyDictionary): ResponseBody {
        val result = iWebSystemService.addCompanyDictionary(company)
        if (result.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return returnSuccess(result)
    }


    /**
     * 修改数据字典
     */
    @Transactional(rollbackFor = [Exception::class])
    @PostMapping("updateDictionAry")
    open fun updateDictionary(@RequestBody company: CompanyDictionary): ResponseBody {
        val result = companyDictionaryService.updateDictionary(company)
        if (result.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return returnSuccess(result)
    }

    /**
     * 删除数据字典
     */
    @Transactional(rollbackFor = [Exception::class])
    @GetMapping("deleteDictionary/{id}")
    open fun deleteDictionary(@PathVariable id: Long): ResponseBody {
        val result = companyDictionaryService.deleteDictionary(id)
        if (result.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return returnSuccess(companyDictionaryService.deleteDictionary(id))
    }

    /**
     * 数据字典列表（分页）
     */
    @PostMapping("getPageList")
    @Transactional
    open fun getPageList(@RequestBody vo: CompanyDictionaryVo): ResponseBody {
        val page = Page<CompanyDictionary>(vo.current, vo.size)
        return returnSuccess(webCompanyService.getDictionaryList(page, vo))
    }


    /**
     * 根据项目名称查询数据字典（实际根据id）
     */
    @GetMapping("getDictionaryById/{id}")
    @Transactional
    open fun getDictionaryById(@PathVariable id: Long): ResponseBody {
        val result = companyDictionaryService.selectDictionaryById(id)
        return returnSuccess(result)
    }

}
